package com.yilin.tms.truck.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.reflect.TypeToken;
import com.yilin.commons.util.JsonUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.base.BaseController;

import com.yilin.tms.core.entity.truck.TransportLicense;
import com.yilin.tms.core.entity.truck.TravelLicense;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.truck.mapper.TransportLicenseMapper;
import com.yilin.tms.truck.mapper.TravelLicenseMapper;
import com.yilin.tms.truck.mapper.TruckSourceMapper;
import com.yilin.tms.truck.mapper.TruckVehicleMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/truck/migration/")
public class MigrationController extends BaseController {
    @Resource
    TruckVehicleMapper truckVehicleMapper;
    @Resource
    TransportLicenseMapper transportLicenseMapper;
    @Resource
    TravelLicenseMapper travelLicenseMapper;

    /**
     * 派单移植
     * */
    @PostMapping("truckVehicleList")
    @SkipLoginVerify
    public ReturnData<Object> truckVehicleList(int page) {
        PageData<TruckVehicle> data = truckVehicleMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TruckVehicle>().orderByAsc("create_time"));
        String toJson = JsonUtil.objectToJson(data.getList());
        List<Map<String, Object>> dataList = JsonUtil.jsonToObject(toJson, new TypeToken<List<Map<String, Object>>>() {});
        for (Map<String, Object> map : dataList) {
            if(map.get("tmsTruckDataState")==null) continue;
            if(map.get("tmsTruckDataState").equals("error")){
                map.put("tmsTruckDataState","wait");
            }
            if(map.get("tmsTruckDataState").equals("creating")){
                map.put("tmsTruckDataState","wait");
            }
            if(map.get("tmsTruckDataState").equals("success")){
                map.put("tmsTruckDataState","complete");
            }
        }
        return jsonView(SUCCESS_CODE, "移植完成",dataList);
    }
    @PostMapping("transportLicenseList")
    @SkipLoginVerify
    public ReturnData<Object> transportLicenseList(int page) {
        PageData<TransportLicense> data = transportLicenseMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TransportLicense>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
    @PostMapping("travelLicenseList")
    @SkipLoginVerify
    public ReturnData<Object> travelLicenseList(int page) {
        PageData<TravelLicense> data = travelLicenseMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TravelLicense>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

















}
